// 亲戚关系计算器
export interface Person {
  id: string
  name: string
  gender: 'male' | 'female'
  generation: number // 辈分，0为基准辈分
  parentId?: string
  spouseId?: string
}

export interface Relationship {
  from: Person
  to: Person
  relation: string
}

// 关系映射表
const relationshipMap: Record<string, Record<string, string>> = {
  // 父系关系
  'father': {
    'father': '爷爷',
    'mother': '奶奶',
    'brother': '伯父/叔父',
    'sister': '姑妈',
    'son': '父亲',
    'daughter': '父亲'
  },
  'mother': {
    'father': '外祖父',
    'mother': '外祖母',
    'brother': '舅舅',
    'sister': '姨妈',
    'son': '母亲',
    'daughter': '母亲'
  },
  // 同辈关系
  'brother': {
    'father': '父亲',
    'mother': '母亲',
    'brother': '兄弟',
    'sister': '姐妹',
    'son': '侄子/外甥',
    'daughter': '侄女/外甥女'
  },
  'sister': {
    'father': '父亲',
    'mother': '母亲',
    'brother': '兄弟',
    'sister': '姐妹',
    'son': '侄子/外甥',
    'daughter': '侄女/外甥女'
  },
  // 子辈关系
  'son': {
    'father': '儿子',
    'mother': '儿子',
    'brother': '儿子',
    'sister': '儿子',
    'son': '孙子',
    'daughter': '孙女'
  },
  'daughter': {
    'father': '女儿',
    'mother': '女儿',
    'brother': '女儿',
    'sister': '女儿',
    'son': '外孙',
    'daughter': '外孙女'
  }
}

// 辈分关系计算
export function calculateRelationship(from: Person, to: Person): string {
  if (from.id === to.id) {
    return '自己'
  }

  // 计算辈分差
  const generationDiff = to.generation - from.generation
  
  // 如果是配偶关系
  if (from.spouseId === to.id) {
    return from.gender === 'male' ? '丈夫' : '妻子'
  }
  if (to.spouseId === from.id) {
    return to.gender === 'male' ? '丈夫' : '妻子'
  }

  // 检查是否是直系血亲
  if (isDirectRelative(from, to)) {
    return calculateDirectRelationship(from, to, generationDiff)
  }

  // 检查是否是旁系血亲
  if (isCollateralRelative(from, to)) {
    return calculateCollateralRelationship(from, to, generationDiff)
  }

  // 更复杂的亲戚关系计算
  return calculateComplexRelationship(from, to)
}

// 检查是否是直系血亲
function isDirectRelative(from: Person, to: Person): boolean {
  // 检查是否是直系血亲（父母、祖父母、子女、孙子女等）
  return isAncestor(from, to) || isDescendant(from, to)
}

// 检查是否是旁系血亲
function isCollateralRelative(from: Person, to: Person): boolean {
  // 检查是否是旁系血亲（兄弟姐妹、堂兄弟姐妹、表兄弟姐妹等）
  const fromAncestors = getAncestors(from)
  const toAncestors = getAncestors(to)
  
  // 如果有共同祖先但不是直系关系，则是旁系
  return fromAncestors.some(ancestor => toAncestors.includes(ancestor)) && 
         !isDirectRelative(from, to)
}

// 获取所有祖先
function getAncestors(person: Person): string[] {
  const ancestors: string[] = []
  let current = person
  
  while (current.parentId) {
    ancestors.push(current.parentId)
    // 这里需要根据实际的家族数据来查找父辈
    // 简化处理，实际应用中需要维护完整的家族树
    break
  }
  
  return ancestors
}

// 检查是否是祖先
function isAncestor(ancestor: Person, descendant: Person): boolean {
  return ancestor.generation > descendant.generation && 
         Math.abs(ancestor.generation - descendant.generation) === 1
}

// 检查是否是后代
function isDescendant(ancestor: Person, descendant: Person): boolean {
  return ancestor.generation < descendant.generation && 
         Math.abs(ancestor.generation - descendant.generation) === 1
}

// 计算直系关系
function calculateDirectRelationship(from: Person, to: Person, generationDiff: number): string {
  if (generationDiff === 1) {
    // 上一辈
    return to.gender === 'male' ? '父亲' : '母亲'
  } else if (generationDiff === -1) {
    // 下一辈
    return to.gender === 'male' ? '儿子' : '女儿'
  } else if (generationDiff === 2) {
    // 上两辈
    return to.gender === 'male' ? '爷爷' : '奶奶'
  } else if (generationDiff === -2) {
    // 下两辈
    return to.gender === 'male' ? '孙子' : '孙女'
  }
  
  return calculateComplexRelationship(from, to)
}

// 计算旁系关系
function calculateCollateralRelationship(from: Person, to: Person, generationDiff: number): string {
  if (generationDiff === 0) {
    // 同辈
    if (from.gender === to.gender) {
      return from.gender === 'male' ? '兄弟' : '姐妹'
    } else {
      return '兄弟姐妹'
    }
  } else if (generationDiff === 1) {
    // 上一辈旁系
    return to.gender === 'male' ? '伯父/叔父' : '姑妈/姨妈'
  } else if (generationDiff === -1) {
    // 下一辈旁系
    return to.gender === 'male' ? '侄子' : '侄女'
  }
  
  return calculateComplexRelationship(from, to)
}

// 复杂亲戚关系计算
function calculateComplexRelationship(from: Person, to: Person): string {
  const generationDiff = to.generation - from.generation
  
  // 根据辈分差和性别组合计算
  if (generationDiff > 2) {
    const level = generationDiff - 2
    let prefix = ''
    let suffix = ''
    
    if (level === 1) {
      prefix = from.gender === 'male' ? '曾' : '外曾'
      suffix = to.gender === 'male' ? '祖父' : '祖母'
    } else if (level === 2) {
      prefix = from.gender === 'male' ? '高' : '外高'
      suffix = to.gender === 'male' ? '祖父' : '祖母'
    } else {
      prefix = from.gender === 'male' ? '高' : '外高'
      suffix = to.gender === 'male' ? '祖父' : '祖母'
    }
    
    return prefix + suffix
  } else if (generationDiff < -2) {
    const level = Math.abs(generationDiff) - 2
    let prefix = ''
    let suffix = ''
    
    if (level === 1) {
      prefix = from.gender === 'male' ? '曾' : '外曾'
      suffix = to.gender === 'male' ? '孙' : '孙女'
    } else if (level === 2) {
      prefix = from.gender === 'male' ? '高' : '外高'
      suffix = to.gender === 'male' ? '孙' : '孙女'
    } else {
      prefix = from.gender === 'male' ? '高' : '外高'
      suffix = to.gender === 'male' ? '孙' : '孙女'
    }
    
    return prefix + suffix
  }

  return '亲戚'
}

// 根据关系描述反向查找可能的称呼
export function findRelationshipByDescription(description: string): string[] {
  const results: string[] = []
  
  // 简单的关键词匹配
  const keywords = {
    '父亲': ['爸爸', '爸', '爹', '父亲'],
    '母亲': ['妈妈', '妈', '娘', '母亲'],
    '爷爷': ['爷爷', '祖父', '阿公'],
    '奶奶': ['奶奶', '祖母', '阿婆'],
    '外祖父': ['外祖父', '外公', '姥爷'],
    '外祖母': ['外祖母', '外婆', '姥姥'],
    '兄弟': ['兄弟', '哥哥', '弟弟', '哥', '弟'],
    '姐妹': ['姐妹', '姐姐', '妹妹', '姐', '妹'],
    '儿子': ['儿子', '儿子', '儿'],
    '女儿': ['女儿', '女儿', '女'],
    '孙子': ['孙子', '孙'],
    '孙女': ['孙女', '孙女'],
    '外孙': ['外孙', '外孙'],
    '外孙女': ['外孙女', '外孙女']
  }

  for (const [relation, terms] of Object.entries(keywords)) {
    if (terms.some(term => description.includes(term))) {
      results.push(relation)
    }
  }

  return results
}

// 创建示例家庭成员数据
export function createSampleFamily(): Person[] {
  return [
    {
      id: '1',
      name: '我',
      gender: 'male',
      generation: 0
    },
    {
      id: '2',
      name: '父亲',
      gender: 'male',
      generation: 1,
      parentId: '4',
      spouseId: '3'
    },
    {
      id: '3',
      name: '母亲',
      gender: 'female',
      generation: 1,
      parentId: '6',
      spouseId: '2'
    },
    {
      id: '4',
      name: '爷爷',
      gender: 'male',
      generation: 2,
      spouseId: '5'
    },
    {
      id: '5',
      name: '奶奶',
      gender: 'female',
      generation: 2,
      spouseId: '4'
    },
    {
      id: '6',
      name: '外祖父',
      gender: 'male',
      generation: 2,
      spouseId: '7'
    },
    {
      id: '7',
      name: '外祖母',
      gender: 'female',
      generation: 2,
      spouseId: '6'
    },
    {
      id: '8',
      name: '兄弟',
      gender: 'male',
      generation: 0,
      parentId: '2'
    },
    {
      id: '9',
      name: '姐妹',
      gender: 'female',
      generation: 0,
      parentId: '2'
    },
    {
      id: '10',
      name: '儿子',
      gender: 'male',
      generation: -1,
      parentId: '1'
    },
    {
      id: '11',
      name: '女儿',
      gender: 'female',
      generation: -1,
      parentId: '1'
    },
    {
      id: '12',
      name: '伯父',
      gender: 'male',
      generation: 1,
      parentId: '4'
    },
    {
      id: '13',
      name: '叔父',
      gender: 'male',
      generation: 1,
      parentId: '4'
    },
    {
      id: '14',
      name: '姑妈',
      gender: 'female',
      generation: 1,
      parentId: '4'
    },
    {
      id: '15',
      name: '舅舅',
      gender: 'male',
      generation: 1,
      parentId: '6'
    },
    {
      id: '16',
      name: '姨妈',
      gender: 'female',
      generation: 1,
      parentId: '6'
    }
  ]
}
